package untils;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class NetworkUtils {


    //获取发送报文
    public static DatagramPacket getSendPacket(String msg,String ip,int port) throws UnknownHostException {
        byte[] bytes=msg.getBytes();
        InetAddress targetIp = InetAddress.getByName(ip);
        DatagramPacket sendPackt =
                new DatagramPacket(bytes, 0, bytes.length, targetIp, port);
        return sendPackt;
    }

    //获取接收报文
    public static DatagramPacket getRecivePacket(){
        byte[] bytes=new byte[1024];
        DatagramPacket RecivePacket =
                new DatagramPacket(bytes, 0, bytes.length);
        return RecivePacket;
    }

    //解析报文
    public static String parseMsg(DatagramPacket datagramPacket){
        byte[] data = datagramPacket.getData();
        int offset = datagramPacket.getOffset();
        int length = datagramPacket.getLength();
        String s=new String(data,offset,length);
        return s;
    }
}
